﻿$(function () {
    refreshCode();
    $(".verifyCode-img").click(function () {
        refreshCode();
    });

    function refreshCode() {
        var img = document.getElementById("imgVerifyCode");
        if (img != null) {
            var currentDate = new Date();
            img.src = '/Customer/VerifyCodeImage?ref=1&t=' + currentDate.getTime();
        }
    }

    //注册表单验证
    $("#registerForm").validate({
        rules: {
            Email: {
                required: true,
                isEmail: true
            },
            CellPhone: {
                required: true,
                isMobile: true
            },
            PassWord: {
                required: true,
                minlength: 6,
                maxlength: 20
            },
            validCode: {
                required: true,
                minlength: 5,
                maxlength: 5
            }
        },
        submitHandler: function (form) {
            var validCode = $.trim($('#validCode').val());
            $.ajax({
                type: "GET",
                url: "CheckVerifyCode?code=" + validCode,
                dataType: "JSON",
                success: function (response) {
                    if (response == false) {
                        $('#validCodeResult').html('验证码不正确');
                        refreshCode();
                        return;
                    } else {
                        $('#validCodeResult').html('');
                        form.submit();
                    }
                },
                error: function (err) {
                    alert(err.responseText);
                }
            });
        }
    });
});